<!-- eslint-disable vue/valid-template-root -->
<template>
  <div id="main">
    <div class="contain">
      <div class="log">
        <img src="../../assets/logo.png" alt="">
      </div>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="60px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username" label-width="100px">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" label-width="100px">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item prop="">
          <el-button class="left_btn" :loading="loading" type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { LoginWay } from '@/api/login'
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    return {
      ruleForm: {
        username: 'admin',
        password: '123456',
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
      },
      redirect: undefined,
      loading: false
    }
  },
  computed: {},
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
        // console.log(route);
      },
      immediate: true
    },
  },
  mounted() { },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        // console.log(valid);
        if (valid) {
          this.loading = true

          let res = await this.$store.dispatch('user/login', this.ruleForm)
          if (res) {
            this.loading = false
            this.$message.success('登录成功');
            this.$router.push('/')
          }


          // try {
          //   let res = await LoginWay(this.ruleForm)
          //   // console.log(res);
          //   if (res.meta.status == 200) {
          //     this.$message.success('登录成功');
          //     SetToken(res.data.token)
          //     this.$router.push({ path: this.redirect || '/' })
          //   }
          // } catch {
          //   console.log('res-----');
          // }
        } else {
          this.loading = false
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style scoped lang="scss">
#main {
  width: 100vm;
  height: 100vh;
  background: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
}

.contain {
  width: 450px;
  height: 304px;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  background-color: #fff;

  .log {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }

  .left_btn {
    margin-left: 140px;
  }

  /deep/ .el-form {
    position: absolute;
    bottom: 22px;
    // padding: 0 20px;
    width: 100%;
    box-sizing: border-box;
  }

  /deep/ .el-input {
    width: 300px;
  }

  /deep/ .el-form-item__content {
    margin: 0;
  }

  /deep/ .el-form-item.is-error .el-input__inner,
  .el-form-item.is-error .el-input__inner:focus,
  .el-form-item.is-error .el-textarea__inner,
  .el-form-item.is-error .el-textarea__inner:focus,
  .el-message-box__input input.invalid,
  .el-message-box__input input.invalid:focus {
    border-color: #67c23a;
  }
}
</style>